package com.gongjian.web.dcontroller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gongjian.database.entity.Activity;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.utile.Hump;
import com.gongjian.web.controller.Html;
import com.gongjian.web.register.ResgisterInfo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author donting
 * @Description
 * @create 2019-04-01 9:20
 */
@RestController
public class DRegisterContoller {

    @Autowired
    SqlSession sqlSession;

    @Autowired
    Html html;

    @RequestMapping("/register/begingRegister")
    public String begingRegister(int activityId, HttpSession session){
       Activity activity= sqlSession.selectOne("begingRegister",activityId);
       // return  html.login(session)
       if(activity!=null){
           System.out.println("保存session》》》"+session.getId());
           session.setAttribute("activity",activity);
           return new CodeEntity(200,"22").toJSONString();
       }else{
           return new CodeEntity(300,"错误，请刷新页面").toJSONString();
       }


    }

    /**
     * 检测 老师是否扫码 进行二维码变换 弃用
     * @param session
     * @return
     */
     @RequestMapping("/register/isBengin")
    public String begingRegister(HttpSession session){

        ResgisterInfo resgisterInfo=(ResgisterInfo)session.getAttribute("resgisterInfo");
        Activity activity=(Activity)session.getAttribute("activity");
        if(resgisterInfo!=null){
            CodeEntity codeEntity=new CodeEntity(200);
            codeEntity.put("latitude",resgisterInfo.getLatitude());
            codeEntity.put("longitude",resgisterInfo.getLongitude());
            codeEntity.put("activityId",activity.getActivityId());
            return  codeEntity.toJSONString();
        }
        else{
            return new CodeEntity(5000).toJSONString();
        }
    }

    /**
     * 获取信息
     * @param session
     * @return
     */
    @RequestMapping("/register/getRsgisterInfo")
    public String getRsgisterInfo(HttpSession session){

        ResgisterInfo resgisterInfo=(ResgisterInfo)session.getAttribute("resgisterInfo");
        Activity activity=(Activity)session.getAttribute("activity");
        CodeEntity codeEntity=new CodeEntity(200);
        codeEntity.put("activityId",activity.getActivityId());
        codeEntity.put("sessionId",session.getId());
        codeEntity.put("latitude",resgisterInfo.getLatitude());
        codeEntity.put("longitude",resgisterInfo.getLongitude());
        return codeEntity.toJSONString();

    }

    /**
     * 获取签到表
     * @param session
     * @return
     */
    @RequestMapping("/register/getRsgisterUser")
    public String getRsgisterUser(HttpSession session){
        Activity activity=(Activity)session.getAttribute("activity");
        List<Map<String,Object>> list=sqlSession.selectList("findRegisterByRegisterId",activity.getActivityId());
        list= Hump.formatHumpNameForList(list);
        CodeEntity codeEntity = new CodeEntity(200,"ok");
        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONStringWithDateFormat(list,timeFormat));  /**list为 List<Map<String, String>>**/
        codeEntity.setData(jsonArray);
      return codeEntity.toJSONString();

    }

    /**
     * 获取 一个活动的签到表
     * @param session
     * @return
     */
    @RequestMapping("/register/getAllRsgisterByActivityId")
    public String getAllRsgisterByActivityId(HttpSession session){
        Activity activity=(Activity)session.getAttribute("activity");
        List<Map<String,Object>> list=sqlSession.selectList("getAllRsgisterByActivityId",activity.getActivityId());
        list= Hump.formatHumpNameForList(list);
        CodeEntity codeEntity = new CodeEntity(200,"ok");
        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONStringWithDateFormat(list,timeFormat));  /**list为 List<Map<String, String>>**/
        codeEntity.setData(jsonArray);
        return codeEntity.toJSONString();

    }

    /**
     * 获取 一个活动的签到表
     * @param session
     * @return
     */
    @RequestMapping("/register/getAllRsgisterByActivityIdsetId")
    public String getAllRsgisterByActivityIdet(int actityId,HttpSession session){

        List<Map<String,Object>> list=sqlSession.selectList("getAllRsgisterByActivityId",actityId);
        list= Hump.formatHumpNameForList(list);
        CodeEntity codeEntity = new CodeEntity(200,"ok");
        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONStringWithDateFormat(list,timeFormat));  /**list为 List<Map<String, String>>**/
        codeEntity.setData(jsonArray);
        return codeEntity.toJSONString();

    }


}
